<template>
	<el-drawer title="投票结果" v-model="visible" :size="800" destroy-on-close @closed="$emit('closed')">
		<el-container v-loading="loading">
			<el-main>
				<div style="text-align: center;font-weight: 600;font-size: 18px">{{form.title}}</div>
				<el-row :gutter="24" style="margin: 15px">
					<el-col :lg="8">
						<el-tag size="small" type="success">{{form.voteTypeName}}</el-tag>
					</el-col>
					<el-col :lg="8" style="text-align: center">
						{{form.beginTime}}~{{form.endTime}}
					</el-col>
					<el-col :lg="8" style="text-align: right">
						<el-tag size="small" type="info">{{form.sendDeptName}}</el-tag>
					</el-col>
				</el-row>
				<div v-html="form.content" style="margin: 15px;font-size: 14px"></div>
				<el-divider content-position="center">投票结果</el-divider>
				<div v-for="(item,index) in resForm" :key="item.itemId" style="font-size: 16px;margin: 15px">
					<div style="font-weight: 600">{{ index + 1 }}、{{ item.title }}</div>
					<div style="padding-left: 20px;">
						<div v-for="(childItem,i) in item.childItem" :key="i" style="margin: 15px">
							{{ i + 1 }}.{{childItem.childTitle }}
							<el-tag size="small" effect="dark" type="success" @click="showUserList(childItem.childId)" style="float: right;cursor: pointer;" v-if="form.isAnonymous=='1'">实名</el-tag>
							<div>
								<el-progress :percentage="getResByItem(childItem.childId, item.result)"></el-progress>
							</div>
						</div>
					</div>
				</div>
				<el-footer style="text-align: center;">
					<el-button @click="visible=false">关闭</el-button>
				</el-footer>
			</el-main>
		</el-container>
	</el-drawer>
	<vote-user v-if="dialog.save" ref="voteUserDialog" @closed="dialog.save=false"></vote-user>
</template>

<script>
import voteUser from '@/views/info/vote/voteuser'
export default {
	emits: ['success', 'closed'],
	components: {
		voteUser
	},
	data() {
		return {
			dialog:{
				save:false
			},
			loading: false,
			voteId:'',
			form: {
			},
			resForm:[],
			visible: false,
			isSaveing: false,
		}
	},
	mounted() {
	},
	methods: {
		showUserList(itemId){
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.voteUserDialog.open().setData({voteId:this.voteId,itemId:itemId})
			})
		},
		async getVoteResult() {
			var res = await this.$API.info.vote.getVoteResult.get({voteId: this.voteId});
			if (res.code == 200) {
				this.resForm = res.data;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		getResByItem(childId, result){

			var zs = 0;
			var resv = 0;
			if (childId != undefined && result != undefined) {
				result.forEach(item => {
					if (childId == item.itemId)
					{
						resv =Number(item.zs);
					}
					zs = zs+Number(item.zs);
				})
			}
			if(resv==0)
			{
				return resv;
			}else{
				return parseFloat((resv/zs)*100).toFixed(2);
			}
		},
		//显示
		open(model) {
			this.model = model;
			this.visible = true;
			return this;
		},
		//表单注入数据
		setData(data) {
			this.voteId = data.voteId
			this.form.title = data.title
			this.form.voteTypeName = data.voteTypeName
			this.form.sendDeptName = data.sendDeptName;
			this.form.beginTime = data.beginTime
			this.form.endTime = data.endTime
			this.form.content=data.content
			this.form.isAnonymous = data.isAnonymous
			this.getVoteResult();
		},
	}
}
</script>

<style>
</style>
